import pandas as pd
from nicegui import ui


def load_titanic_data():
    """加载Titanic数据集"""
    try:
        df = pd.read_csv('data/train.csv')
        ui.notify("✅ Titanic数据加载成功！", type='positive')
        return df
    except Exception as e:
        ui.notify(f"❌ 数据加载失败: {str(e)}", type='negative')
        return None


def get_column_info(df):
    """获取数据集列信息"""
    if df is None:
        return []

    column_info = []
    for col in df.columns:
        dtype = str(df[col].dtype)
        unique_count = df[col].nunique()
        na_count = df[col].isna().sum()

        column_info.append({
            'name': col,
            'dtype': dtype,
            'unique': unique_count,
            'missing': na_count,
            'missing_percent': round(na_count / len(df) * 100, 2)
        })

    return column_info